package com.tianyuan.dplrf.ui.activity;

import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.WrapRecyclerView;
import com.tianyuan.dplrf.R;
import com.tianyuan.dplrf.app.AppActivity;
import com.tianyuan.dplrf.ui.adapter.Nczd_zddetAdapter;
import com.tianyuan.dplrf.ui.bean.ZdshowBean;
import com.tianyuan.dplrf.ui.common.SpaceItemDecoration;
import com.xuexiang.xutil.display.DensityUtils;

import java.util.ArrayList;
import java.util.List;

public class ChoosezdpageActivity extends AppActivity implements View.OnClickListener {
    private ImageView img_land_back;
    private TextView tv_title;
    private ImageView img_showdia;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_chosezdpage;
    }

    @Override
    protected void initView() {

        img_land_back = findViewById(R.id.img_land_back);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setVisibility(View.GONE);
        img_land_back.setOnClickListener(this);


        img_showdia = findViewById(R.id.img_showdia);
        img_showdia.setOnClickListener(this);
    }

    private List<ZdshowBean> mlist;

    @Override
    protected void initData() {
        mlist = new ArrayList<>();
        for (int a = 0; a < 9; a++) {
            ZdshowBean bean = new ZdshowBean();
            if (a % 3 == 0) {
                bean.setBuystate(true);
            } else {
                bean.setBuystate(false);
            }
            bean.setDescrip("");
            bean.setName("城市农场" + String.format("%02d", a + 1));
            mlist.add(bean);
        }
        initPopWindow();
        initPopList();


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_land_back:
                finish();
                break;
            case R.id.img_showdia:
                showPop1();
                break;
        }
    }

    protected void initPopList() {
        View pop = LayoutInflater.from(this).inflate(R.layout.pop_zd_ncxuandi, getContentView(), false);
        mPopupWindow2 = new PopupWindow(pop, DensityUtils.dip2px(340), ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopupWindow2.setAnimationStyle(R.style.mypopwindow_anim_style);
        mPopupWindow2.setOutsideTouchable(false);


        TextView tv_notice = pop.findViewById(R.id.tv_notice);
        ImageView img_close = pop.findViewById(R.id.img_close);
        WrapRecyclerView recy_pop_nclb = pop.findViewById(R.id.recy_pop_nclb);
        Nczd_zddetAdapter zddetAdapter = new Nczd_zddetAdapter(getApplicationContext());
        zddetAdapter.setData(mlist);

        zddetAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                if (!mlist.get(position).isBuystate()) {
                    if (mPopupWindow2 != null) {
                        mPopupWindow2.dismiss();
                    }
                    choosedBean = mlist.get(position);
                    showPop1();
                }
            }
        });
        recy_pop_nclb.addItemDecoration(new SpaceItemDecoration(DensityUtils.dip2px(6)));
        recy_pop_nclb.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));
        recy_pop_nclb.setAdapter(zddetAdapter);
        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPopupWindow2 != null) {
                    mPopupWindow2.dismiss();
                }
                showPop1();
            }
        });

    }

    private ZdshowBean choosedBean;
    private void showPop1() {
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            mPopupWindow.showAtLocation(getContentView(), Gravity.RIGHT, 0, 0);
            if (choosedBean != null){
                TextView textView = mPopupWindow.getContentView().findViewById(R.id.tv_ncname);
                textView.setText(choosedBean.getName());
            }else {
                TextView textView = mPopupWindow.getContentView().findViewById(R.id.tv_ncname);
                textView.setText("城市农场002");
            }

        }
    }

    private void showPop2() {
        if (mPopupWindow2 != null && !mPopupWindow2.isShowing()) {
            mPopupWindow2.showAtLocation(getContentView(), Gravity.RIGHT, 0, 0);
        }
    }

    private PopupWindow mPopupWindow;
    private PopupWindow mPopupWindow2;

    public void initPopWindow() {
        View pop = LayoutInflater.from(this).inflate(R.layout.pop_zd_xiadanpage, getContentView(), false);
        mPopupWindow = new PopupWindow(pop, DensityUtils.dip2px(340), ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        mPopupWindow.setOutsideTouchable(false);
//        mPopupWindow.setClippingEnabled(false);//刘海屏是否要显示到

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                backgroundAlpha(1f);
            }
        });
        TextView tv_ad1 = pop.findViewById(R.id.tv_ad1);
        TextView tv_ncname = pop.findViewById(R.id.tv_ncname);
        TableRow tab_xuandi = pop.findViewById(R.id.tab_xuandi);
        TextView tv_detitl = pop.findViewById(R.id.tv_detitl);
        TextView tv_tishi = pop.findViewById(R.id.tv_tishi);
        AppCompatCheckBox check_zudihetong = pop.findViewById(R.id.check_zudihetong);
        TextView tv_zudihetong = pop.findViewById(R.id.tv_zudihetong);
        AppCompatButton btn_xiadan = pop.findViewById(R.id.btn_xiadan);
        tab_xuandi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
                showPop2();
            }
        });
        btn_xiadan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (check_zudihetong.isChecked()) {
                    Intent intent = new Intent(ChoosezdpageActivity.this, PayLandActivity.class);
                    intent.putExtra("act_name", ChoosezdpageActivity.class.getSimpleName());
                    startActivity(intent);
//                    toast("下单成功");
                } else {
                    toast("请阅读相关合同");
                }

            }
        });


        postDelayed(new Runnable() {
            @Override
            public void run() {
                if (img_land_back.getWidth() > 0) {
                    showPop1();
                } else {
                    post(this);
                }
            }
        }, 100);


    }

    @Override
    protected void onDestroy() {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
        if (mPopupWindow2.isShowing()) {
            mPopupWindow2.dismiss();
        }
        super.onDestroy();
    }
}